package com.mujin.keji.collectionsystem.module.mypager.views;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mujin.keji.collectionsystem.R;
import com.mujin.keji.collectionsystem.base.BaseActivity;
import com.mujin.keji.collectionsystem.base.BaseDto;
import com.mujin.keji.collectionsystem.listeners.CustomItemClickListener;
import com.mujin.keji.collectionsystem.module.mypager.adapter.VersionListAdapter;
import com.mujin.keji.collectionsystem.module.mypager.dto.VersionDto;
import com.mujin.keji.collectionsystem.module.mypager.dto.VersionListDto;
import com.mujin.keji.collectionsystem.module.mypager.viewmodel.AboutViewModel;
import com.mujin.keji.collectionsystem.utils.DisplayUtil;
import com.mujin.keji.collectionsystem.utils.EmptyUtils;
import com.mujin.keji.collectionsystem.widget.CustomerItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 历史版本列表
 *
 * @author weishixiong
 * @Time 2018-07-12
 */

public class HistoryVersionListActivity extends BaseActivity implements CustomItemClickListener {
    private SmartRefreshLayout srHasData;
    private RecyclerView rvHistoryList;
    private LinearLayout llNoData;
    private SmartRefreshLayout srNoData;
    private TextView tvNoData;
    private AboutViewModel aboutViewModel;
    private VersionListAdapter adapter;
    private List<VersionListDto> versionListDtoList;
    private LinearLayoutManager linearLayoutManager;
    private RelativeLayout rlHasData;

    @Override
    protected void createViewModel() {
        aboutViewModel = ViewModelProviders.of(this).get(AboutViewModel.class);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_historyversionlist;
    }

    @Override
    public void initView() {
        setCenterTitle("历史版本", "#FFFFFF");
        setleftImage(R.drawable.image_arrow_left_white, true, null);
        rlHasData = (RelativeLayout) findViewById(R.id.rl_has_data);
        srHasData = (SmartRefreshLayout) findViewById(R.id.sr_has_data);
        rvHistoryList = (RecyclerView) findViewById(R.id.rvHistoryList);
        llNoData = (LinearLayout) findViewById(R.id.ll_no_data);
        srNoData = (SmartRefreshLayout) findViewById(R.id.sr_no_data);
        tvNoData = (TextView) findViewById(R.id.tv_no_data);
        srHasData.setEnableLoadMore(false);
        linearLayoutManager = new LinearLayoutManager(this);
        rvHistoryList.setLayoutManager(linearLayoutManager);
        rvHistoryList.addItemDecoration(new CustomerItemDecoration(DisplayUtil.dip2px(this, 8)));
        versionListDtoList = new ArrayList<>();
        adapter = new VersionListAdapter(this, versionListDtoList, this);
        rvHistoryList.setAdapter(adapter);

    }

    @Override
    public void initData() {
        srHasData.autoRefresh();
    }

    /**
     * 获取历史版本
     */
    private void getVersionData() {
        aboutViewModel.getVersionLiveData().observe(this, new Observer<BaseDto<VersionDto>>() {
            @Override
            public void onChanged(@Nullable BaseDto<VersionDto> versionDtoBaseDto) {
                srHasData.finishRefresh();
                srNoData.finishRefresh();
                if (EmptyUtils.isNotEmpty(versionDtoBaseDto.getData()) && EmptyUtils.isNotEmpty(versionDtoBaseDto.getData().getLists())) {
                    rlHasData.setVisibility(View.VISIBLE);
                    llNoData.setVisibility(View.GONE);
                    versionListDtoList.clear();
                    versionListDtoList.addAll(versionDtoBaseDto.getData().getLists());
                    adapter.notifyDataSetChanged();
                } else {
                    rlHasData.setVisibility(View.GONE);
                    llNoData.setVisibility(View.VISIBLE);
                    tvNoData.setText("暂无历史版本");
                }
            }
        });
    }

    @Override
    public void initListener() {
        srHasData.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getVersionData();
            }
        });

        srNoData.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getVersionData();
            }
        });
    }

    @Override
    public void onItemClick(View view, int position) {
        Intent intent = new Intent(this, HistoryDetailActivity.class);
        intent.putExtra("versionListDto", versionListDtoList.get(position));
        startActivity(intent);
    }
}
